package ServletDemo;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @Author: ylc
 * @Description:Servlet常用方法
 * @Date Created in 2022-05-26 21:36
 */
public class AServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Date date = new Date();
        /**
         * void setAttribute(String name, Object obj); // 向域当中绑定数据。
         * Object getAttribute(String name); // 从域当中根据name获取数据。
         * void removeAttribute(String name); // 将域当中绑定的数据移除
         */
        request.setAttribute("system",date);
        /**
         * 	Map<String,String[]> parameterMap = request.getParameterMap();
         * 	Enumeration<String> names = request.getParameterNames();
         * 	String[] values = request.getParameterValues("name");
         * 	String value = request.getParameter("name");
         */
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 设置请求体字符集，处理POST请求的乱码问题
        request.setCharacterEncoding("UTF-8");

        // 解决响应乱码问题
        response.setContentType("text/html;charset=UTF-8");

        // 获取应用的根路径
        String contextPath = request.getContextPath();  //  /Study12

        // 获取请求方式
        String method = request.getMethod();

        // 获取请求的URI
        String uri = request.getRequestURI();  // /Study12/a
        // 获取servlet path
        String servletPath = request.getServletPath(); //   /a

        System.out.println(username);
        System.out.println(password);
        System.out.println(contextPath);
        System.out.println(method);
        System.out.println(uri);
        System.out.println(servletPath);

        // 请求转发器用于两个Servlet之间共享数据
        request.getRequestDispatcher("/b").forward(request,response);

    }
}
